package com.inspur.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FilesShow
 */
public class FilesShow extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FilesShow() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String imgPath = request.getParameter("path");
		InputStream is = null;
		OutputStream os = null;
		String realPath = request.getSession().getServletContext().getRealPath("/");
		String userPicPath = realPath+".."+imgPath;
		try {
			is = new  FileInputStream(new File(userPicPath));
			os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length = 0;
			while((length = is.read(b)) != -1){
				os.write(b, 0, length);
			}
			is.close();
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(is != null){
				is.close();
			}
			if(os != null){
				os.close();
			}
		}
	}

}
